﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections;
using Portal.Cms.Model;
using Portal.Web.Utility;
using Portal.Cms.Bussiness;
using Portal.Web.Model;
using Portal.Cms.Utility;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class ChangeAttachDocFileRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string DataId = (string)WebEnvironments.Request(SiteId, "id");
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                string DocCaption = "Tài liệu gắn kèm:";
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                string Html =
                     "<script language=javascript>\r\n " +
                     "function fileSelectedChangeAttachDoc() {\r\n" +
                     "    var file = document.getElementById('fileToUploadChangeAttachDoc').files[0];\r\n" +
                     "    if (file) {\r\n" +
                     "        var fileSize = 0;\r\n" +
                     "        if (file.size > 1024 * 1024)\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';\r\n" +
                     "        else\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';\r\n" +
                     "        document.getElementById('fileNameChangeAttachDoc').innerHTML = 'Tên tệp: ' + file.name;\r\n" +
                     "        document.getElementById('fileSizeChangeAttachDoc').innerHTML = 'Kích cỡ: ' + fileSize;\r\n" +
                     "        document.getElementById('fileTypeChangeAttachDoc').innerHTML = 'Loại tệp: ' + file.type;\r\n" +
                     "    }\r\n" +
                     "}\r\n" +

                     "function uploadFileChangeAttachDoc() {\r\n" +
                     "    if(document.getElementById('fileToUploadChangeAttachDoc').value==''){\r\n" +
                     "       document.getElementById('txtUploadFileChangeAttachDoc').value='';\r\n" +
                     "       CallSave();\r\n" +
                     "       return;\r\n" +
                     "   }\r\n" +
                     "    DataId = document.getElementById('txtChangeAttachDocUpdateId').value;\r\n" +
                     "    var fd = new FormData();\r\n" +
                     "    fd.append(\"fileToUpload\", document.getElementById('fileToUploadChangeAttachDoc').files[0]);\r\n" +
                     "    var xhr = new XMLHttpRequest();\r\n" +
                     "    xhr.upload.addEventListener(\"progress\", uploadProgressChangeAttachDoc, false);\r\n" +
                     "    xhr.addEventListener(\"load\", uploadCompleteChangeAttachDoc, false);\r\n" +
                     "    xhr.addEventListener(\"error\", uploadFailedChangeAttachDoc, false);\r\n" +
                     "    xhr.addEventListener(\"abort\", uploadCanceledChangeAttachDoc, false);\r\n" +
                     "    xhr.open(\"POST\", \"CmsUploadAttachFileHandler.ashx?DataId=\"+DataId+\"&sid=" + SiteId + "&uni=" + AssetCode + "&gid=" + GroupDataId + "\");\r\n" +
                     "    xhr.send(fd);\r\n" +
                     "}\r\n" +

                     "function uploadProgressChangeAttachDoc(evt) {\r\n" +
                     "    if (evt.lengthComputable) {\r\n" +
                     "    }\r\n" +
                     "    else {\r\n" +
                     "        document.getElementById('progressNumberChangeAttachDoc').innerHTML = 'Không tính toán được thời gian';\r\n" +
                     "    }\r\n" +
                     "}\r\n" +


                     " function CallSave()\r\n" +
                     " {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      DataId = '"+DataId+"';\r\n" +
                     "      uploadFile = document.getElementById('txtUploadFileChangeAttachDoc').value;\r\n" +
                     "      ParentDataId='" + OCmsData.ParentDataId + "';\r\n" +
                     "      AjaxOut = Portal.Cms.Render.ChangeAttachDocFileRenderBll.ServerSideSaveAttachDoc(SiteId,'" + GroupDataId + "',ParentDataId, DataId, uploadFile).value;\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     " }\r\n" +


                     "function uploadCompleteChangeAttachDoc(evt) {\r\n" +
                     "    /* This event is raised when the server send back a response */\r\n" +
                     "    document.getElementById('txtUploadFileChangeAttachDoc').value=evt.target.responseText;\r\n" +
                     "    //doan nay chay upload vao day...\r\n" +
                     "    CallSave();\r\n" +
                     "   }\r\n  " +

                       "function uploadFailedChangeAttachDoc(evt) {\r\n" +
                     "       alert(\"Lỗi khi tải tệp dữ liệu lên máy chủ.\");\r\n" +
                     "   }\r\n" +

                     "   function uploadCanceledChangeAttachDoc(evt) {\r\n" +
                     "        alert(\"Người dùng hủy bỏ tải lên máy chủ hoặc mất kết nối với máy chủ.\");\r\n" +
                     "   }\r\n" +
                     "  </script>\r\n" +
                     "  <input type=hidden id=\"txtChangeAttachDocUpdateId\" value=\""+DataId+"\">\r\n" +
                     "  <h3>Thay đổi -  " + DocCaption + "</h3>\r\n" +
                     "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "         <td class=col1>" + DocCaption + "</td>\r\n" +
                   "         <td><input type=hidden id=\"txtUploadFileChangeAttachDoc\"><input type=\"file\" name=\"fileToUploadChangeAttachDoc[]\" id=\"fileToUploadChangeAttachDoc\" onchange=\"fileSelectedChangeAttachDoc();\" /></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "           <td></td>\r\n" +
                   "           <td>\r\n" +
                   "               <div id=\"progressNumberChangeAttachDoc\"></div>\r\n" +
                   "               <div id=\"fileNameChangeAttachDoc\">\r\n" +
                   "               </div>\r\n" +
                   "               <div id=\"fileSizeChangeAttachDoc\">\r\n" +
                   "               </div>\r\n" +
                   "               <div id=\"fileTypeChangeAttachDoc\">\r\n" +
                   "               </div>\r\n" +
                   "           </td>\r\n" +
                   "   </tr>\r\n" +
                   "   <tr>\r\n" +
                   "            <td class=c1></td>\r\n" +
                   "            <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:uploadFileChangeAttachDoc();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:window.open('DocumentInputDataTreeView.aspx?gid="+GroupDataId+"','_self');\"></td>\r\n" +
                   "   </tr>\r\n" +
                   "  </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveAttachDoc(string SiteId, string GroupDataId, string ParentDataId, string DataId, string uploadFile)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ChangeAttachDocFile(SiteId, DataId, uploadFile);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                OAjaxOut.RetUrl = "DocumentInputDataTreeView.aspx?gid="+GroupDataId;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

    }
}
